
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
    <meta charset="UTF-8">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>2.2：路由管理 · 《Flutter实战》</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="baidu-site-verification" content="Y34sAqDKUH"/>
    <meta name="theme-color" content="#ffffff">
    <!--<meta name="description" content="">-->
    <meta name="description" content="《Flutter实战》是Flutter中文网（https://flutterchina.club）开源的电子书，旨在帮助开发者系统化的了解并学习Flutter。">
    <meta name="keywords" content="flutter实战,flutter书籍,flutter电子书,flutter中文网 ">
    <meta name="generator" content="GitBook 3.2.3">
    
    <meta name="author" content="wendux 824783146@qq.com">
    
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/style.css">

    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-search-pro/search.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-ace/ace.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-splitter/splitter.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-prism/prism.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-donate/plugin.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-fontsettings/website.css">
    
    
    

    

    
        
    
        
    
        
    
        
    
        
    

    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://pcdn.flutterchina.club/gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="https://pcdn.flutterchina.club/gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="flutter_package_mgr.html" />
    
    
    <link rel="prev" href="first_flutter_app.html" />
    

    <style>
        .pull-right > i {
            font-size: 25px;
            color: #777 !important;
            cursor: pointer;

        }

        .pull-right > i:hover {
            color: #777 !important;
        }

        body {
            font-size: 15px !important;
        }

        a[href='../todo.html'], a[href='todo.html'] {
            color: #888 !important;
        }

        .copyright {
            text-align: center;
            margin: 80px 16px 8px 16px;
            padding-top: 16px;
            border-top: #eee 1px solid;
            color: grey;
            font-size: .9em;
        }

        .f-links a{
            text-decoration: underline;
            color: dodgerblue !important;
        }

        .f-links a:hover{
            opacity: .8 !important;
        }

        /*.ad {*/
            /*color: white;*/
            /*text-align: center;*/
            /*background: url(https://img.alicdn.com/tfs/TB1Huu7vMmTBuNjy1XbXXaMrVXa-2880-400.png);*/
            /*background-size: cover;*/
            /*cursor: pointer;*/
            /*margin-bottom: 30px;*/
            /*padding: 18px*/
        /*}*/

        /*.ad:hover {*/
            /*opacity: .8;*/
        /*}*/

        .buy-btn {
            text-decoration: none !important;
            padding: 8px 18px;
            border: #268bd2 1px solid !important;
            display: inline-block !important;
            border-radius: 25px;
            color: #268bd2;
            margin-top: 10px;
        }

        .buy-btn:hover {
            transition: all ease 200ms;
            box-shadow: #555 2px 5px 10px;
            background: #eee;
            color: dodgerblue !important;
        }

        .maoyun{
            padding-top: 5px;
        }


    </style>
</head>
<body>
<!--<div id="ad" style="display: none">-->
<!--<div class="ad ad0"-->
<!--onclick="_track('aliyun','https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=8hhi4dts')">-->
<!--<div style="font-size: 1.3em; padding-right: 120px; position: relative; top: 12px">阿里云新春特惠</div>-->
<!--<div style="color:orange; font-size: 13px">限时红包 最高￥<span style="font-size: 40px">1888</span></div>-->
<!--</div>-->
<!--<div class="ad ad1"-->
<!--onclick="_track('aliyun-puhui','https://promotion.aliyun.com/ntms/act/qwbk.html?spm=5176.11533457.1089570.10.74e877e33jtaGy&userCode=8hhi4dts')">-->
<!--<div style="font-size: 1.5em;">阿里云全民云计算-新春特惠</div>-->
<!--<div style="color:orange; font-size: 18px;padding-top: 4px;">普惠上云，云服务器1核1G仅需293元/年</div>-->
<!--</div>-->
<!--</div>-->

<div class="ad"  style="text-align:center; padding-bottom:30px; display: none"><a href="https://item.jd.com/12816296.html" onclick="buy('img')" title='点击购买' target="_blank"><img style="height:250px;box-shadow: #aaa 5px 5px 10px;" src="https://pcdn.flutterchina.club/imgs/book.png"/></a>  <br/> <a class="buy-btn" onclick="buy('img')" href="https://item.jd.com/12816296.html" title='点击购买' target="_blank"> 购买实体书 </a></div>


<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://flutterchina.club" target="_blank" class="custom-link"><div><img height=20 style='position:relative; top:5px; padding-right:5px ' src='https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/favicon.png'/> Flutter中文网</div></a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../intro.html">
            
                <a href="../intro.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">入门篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章：起步
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../chapter1/mobile_development_intro.html">
            
                <a href="../chapter1/mobile_development_intro.html">
            
                    
                    1.1：移动开发技术简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../chapter1/flutter_intro.html">
            
                <a href="../chapter1/flutter_intro.html">
            
                    
                    1.2：初识Flutter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../chapter1/install_flutter.html">
            
                <a href="../chapter1/install_flutter.html">
            
                    
                    1.3：搭建Flutter开发环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../chapter1/dart.html">
            
                <a href="../chapter1/dart.html">
            
                    
                    1.4：Dart语言简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="./">
            
                <a href="./">
            
                    
                    第二章：第一个Flutter应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="first_flutter_app.html">
            
                <a href="first_flutter_app.html">
            
                    
                    2.1：计数器示例
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.2.2" data-path="flutter_router.html">
            
                <a href="flutter_router.html">
            
                    
                    2.2：路由管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3" data-path="flutter_package_mgr.html">
            
                <a href="flutter_package_mgr.html">
            
                    
                    2.3：包管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.4" data-path="flutter_assets_mgr.html">
            
                <a href="flutter_assets_mgr.html">
            
                    
                    2.4：资源管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.5" data-path="flutter_app_debug.html">
            
                <a href="flutter_app_debug.html">
            
                    
                    2.5：调试Flutter APP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.6" data-path="thread_model_and_error_report.html">
            
                <a href="thread_model_and_error_report.html">
            
                    
                    2.6：Dart线程模型及异常捕获
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章：基础组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../chapter3/flutter_widget_intro.html">
            
                <a href="../chapter3/flutter_widget_intro.html">
            
                    
                    3.1：Widget简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../chapter3/state_manage.html">
            
                <a href="../chapter3/state_manage.html">
            
                    
                    3.2：状态管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="../chapter3/text.html">
            
                <a href="../chapter3/text.html">
            
                    
                    3.3：文本、字体样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.4" data-path="../chapter3/buttons.html">
            
                <a href="../chapter3/buttons.html">
            
                    
                    3.4：按钮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.5" data-path="../chapter3/img_and_icon.html">
            
                <a href="../chapter3/img_and_icon.html">
            
                    
                    3.5：图片和Icon
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.6" data-path="../chapter3/radio_and_checkbox.html">
            
                <a href="../chapter3/radio_and_checkbox.html">
            
                    
                    3.6：单选框和复选框
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.7" data-path="../chapter3/input_and_form.html">
            
                <a href="../chapter3/input_and_form.html">
            
                    
                    3.7：输入框和表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.8" data-path="../chapter3/progress.html">
            
                <a href="../chapter3/progress.html">
            
                    
                    3.8：进度指示器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章：布局类组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../chapter4/intro.html">
            
                <a href="../chapter4/intro.html">
            
                    
                    4.1：布局类组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../chapter4/row_and_column.html">
            
                <a href="../chapter4/row_and_column.html">
            
                    
                    4.2：线性布局（Row、Column）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../chapter4/flex.html">
            
                <a href="../chapter4/flex.html">
            
                    
                    4.3：弹性布局（Flex）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="../chapter4/wrap_and_flow.html">
            
                <a href="../chapter4/wrap_and_flow.html">
            
                    
                    4.4：流式布局（Wrap、Flow）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.5" data-path="../chapter4/stack.html">
            
                <a href="../chapter4/stack.html">
            
                    
                    4.5：层叠布局（Stack、Positioned）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.6" data-path="../chapter4/alignment.html">
            
                <a href="../chapter4/alignment.html">
            
                    
                    4.6：对齐与相对定位（Align）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章：容器类组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../chapter5/padding.html">
            
                <a href="../chapter5/padding.html">
            
                    
                    5.1：填充（Padding）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="../chapter5/constrainedbox_and_sizebox.html">
            
                <a href="../chapter5/constrainedbox_and_sizebox.html">
            
                    
                    5.2：尺寸限制类容器（ConstrainedBox等）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.3" data-path="../chapter5/decoratedbox.html">
            
                <a href="../chapter5/decoratedbox.html">
            
                    
                    5.3：装饰容器（DecoratedBox）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.4" data-path="../chapter5/transform.html">
            
                <a href="../chapter5/transform.html">
            
                    
                    5.4：变换（Transform）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.5" data-path="../chapter5/container.html">
            
                <a href="../chapter5/container.html">
            
                    
                    5.5：Container容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.6" data-path="../chapter5/material_scaffold.html">
            
                <a href="../chapter5/material_scaffold.html">
            
                    
                    5.6：Scaffold、TabBar、底部导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.7" data-path="../chapter5/clip.html">
            
                <a href="../chapter5/clip.html">
            
                    
                    5.7：剪裁（Clip）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章：可滚动组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../chapter6/intro.html">
            
                <a href="../chapter6/intro.html">
            
                    
                    6.1：可滚动组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="../chapter6/single_child_scrollview.html">
            
                <a href="../chapter6/single_child_scrollview.html">
            
                    
                    6.2：SingleChildScrollView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.3" data-path="../chapter6/listview.html">
            
                <a href="../chapter6/listview.html">
            
                    
                    6.3：ListView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.4" data-path="../chapter6/gridview.html">
            
                <a href="../chapter6/gridview.html">
            
                    
                    6.4：GridView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.5" data-path="../chapter6/custom_scrollview.html">
            
                <a href="../chapter6/custom_scrollview.html">
            
                    
                    6.5：CustomScrollView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.6" data-path="../chapter6/scroll_controller.html">
            
                <a href="../chapter6/scroll_controller.html">
            
                    
                    6.6：滚动监听及控制（ScrollController）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章：功能型组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.7.1" data-path="../chapter7/willpopscope.html">
            
                <a href="../chapter7/willpopscope.html">
            
                    
                    7.1：导航返回拦截（WillPopScope）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.2" data-path="../chapter7/inherited_widget.html">
            
                <a href="../chapter7/inherited_widget.html">
            
                    
                    7.2：数据共享（InheritedWidget）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.3" data-path="../chapter7/provider.html">
            
                <a href="../chapter7/provider.html">
            
                    
                    7.3： 跨组件状态共享（Provider）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.4" data-path="../chapter7/theme.html">
            
                <a href="../chapter7/theme.html">
            
                    
                    7.4：颜色和主题（Theme）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.5" data-path="../chapter7/futurebuilder_and_streambuilder.html">
            
                <a href="../chapter7/futurebuilder_and_streambuilder.html">
            
                    
                    7.5：异步UI更新（FutureBuilder、StreamBuilder）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.6" data-path="../chapter7/dailog.html">
            
                <a href="../chapter7/dailog.html">
            
                    
                    7.6：对话框详解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">进阶篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../chapter8/">
            
                <a href="../chapter8/">
            
                    
                    第八章：事件处理与通知
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../chapter8/listener.html">
            
                <a href="../chapter8/listener.html">
            
                    
                    8.1：原始指针事件处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../chapter8/gesture.html">
            
                <a href="../chapter8/gesture.html">
            
                    
                    8.2：手势识别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../chapter8/eventbus.html">
            
                <a href="../chapter8/eventbus.html">
            
                    
                    8.3：全局事件总线
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../chapter8/notification.html">
            
                <a href="../chapter8/notification.html">
            
                    
                    8.4：通知(Notification)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../chapter9/">
            
                <a href="../chapter9/">
            
                    
                    第九章：动画
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../chapter9/intro.html">
            
                <a href="../chapter9/intro.html">
            
                    
                    9.1：Flutter动画简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../chapter9/animation_structure.html">
            
                <a href="../chapter9/animation_structure.html">
            
                    
                    9.2：动画结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../chapter9/route_transition.html">
            
                <a href="../chapter9/route_transition.html">
            
                    
                    9.3：自定义路由过渡动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="../chapter9/hero.html">
            
                <a href="../chapter9/hero.html">
            
                    
                    9.4：Hero动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.5" data-path="../chapter9/stagger_animation.html">
            
                <a href="../chapter9/stagger_animation.html">
            
                    
                    9.5：交织动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.6" data-path="../chapter9/animated_switcher.html">
            
                <a href="../chapter9/animated_switcher.html">
            
                    
                    9.6：通用“动画切换”组件（AnimatedSwitcher）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.7" data-path="../chapter9/animated_widgets.html">
            
                <a href="../chapter9/animated_widgets.html">
            
                    
                    9.7：动画过渡组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../chapter10/">
            
                <a href="../chapter10/">
            
                    
                    第十章：自定义组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../chapter10/intro.html">
            
                <a href="../chapter10/intro.html">
            
                    
                    10.1：自定义组件方法简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../chapter10/combine.html">
            
                <a href="../chapter10/combine.html">
            
                    
                    10.2：组合现有组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../chapter10/turn_box.html">
            
                <a href="../chapter10/turn_box.html">
            
                    
                    10.3：组合实例：TurnBox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../chapter10/custom_paint.html">
            
                <a href="../chapter10/custom_paint.html">
            
                    
                    10.4：自绘组件（CustomPaint与Canvas）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../chapter10/gradient_circular_progress_demo.html">
            
                <a href="../chapter10/gradient_circular_progress_demo.html">
            
                    
                    10.5：自绘实例：圆形渐变进度条(自绘)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../chapter11/">
            
                <a href="../chapter11/">
            
                    
                    第十一章：文件操作与网络请求
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="../chapter11/file_operation.html">
            
                <a href="../chapter11/file_operation.html">
            
                    
                    11.1：文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="../chapter11/http.html">
            
                <a href="../chapter11/http.html">
            
                    
                    11.2：Http请求-HttpClient
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="../chapter11/dio.html">
            
                <a href="../chapter11/dio.html">
            
                    
                    11.3：Http请求-Dio package
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="../chapter11/download_with_chunks.html">
            
                <a href="../chapter11/download_with_chunks.html">
            
                    
                    11.4：实例：Http分块下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="../chapter11/websocket.html">
            
                <a href="../chapter11/websocket.html">
            
                    
                    11.5：WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.6" data-path="../chapter11/socket.html">
            
                <a href="../chapter11/socket.html">
            
                    
                    11.6：使用Socket API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.7" data-path="../chapter11/json_model.html">
            
                <a href="../chapter11/json_model.html">
            
                    
                    11.7：Json转Dart Model类
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../chapter12/">
            
                <a href="../chapter12/">
            
                    
                    第十二章：包与插件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../chapter12/develop_package.html">
            
                <a href="../chapter12/develop_package.html">
            
                    
                    12.1：开发package
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.2" data-path="../chapter12/platform-channel.html">
            
                <a href="../chapter12/platform-channel.html">
            
                    
                    12.2：平台通道简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.3" data-path="../chapter12/develop_plugin.html">
            
                <a href="../chapter12/develop_plugin.html">
            
                    
                    12.3：开发Flutter插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.4" data-path="../chapter12/android_implement.html">
            
                <a href="../chapter12/android_implement.html">
            
                    
                    12.4：插件开发：实现Android端API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.5" data-path="../chapter12/ios_implement.html">
            
                <a href="../chapter12/ios_implement.html">
            
                    
                    12.5：插件开发：实现IOS端API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.6" data-path="../chapter12/texture_platformview.html">
            
                <a href="../chapter12/texture_platformview.html">
            
                    
                    12.6：Texture和PlatformView
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../chapter13/">
            
                <a href="../chapter13/">
            
                    
                    第十三章：国际化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="../chapter13/multi_languages_support.html">
            
                <a href="../chapter13/multi_languages_support.html">
            
                    
                    13.1：让App支持多语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.2" data-path="../chapter13/locallization_implement.html">
            
                <a href="../chapter13/locallization_implement.html">
            
                    
                    13.2：实现Localizations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.3" data-path="../chapter13/intl.html">
            
                <a href="../chapter13/intl.html">
            
                    
                    13.3：使用Intl包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.4" data-path="../chapter13/faq.html">
            
                <a href="../chapter13/faq.html">
            
                    
                    13.4：国际化常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../chapter14/">
            
                <a href="../chapter14/">
            
                    
                    第十四章：Flutter核心原理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.7.1" data-path="../chapter14/flutter_ui_system.html">
            
                <a href="../chapter14/flutter_ui_system.html">
            
                    
                    14.1：Flutter UI系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.2" data-path="../chapter14/element_buildcontext.html">
            
                <a href="../chapter14/element_buildcontext.html">
            
                    
                    14.2：Element和BuildContext
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.3" data-path="../chapter14/render_object.html">
            
                <a href="../chapter14/render_object.html">
            
                    
                    14.3：RenderObject与RenderBox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.4" data-path="../chapter14/flutter_app_startup.html">
            
                <a href="../chapter14/flutter_app_startup.html">
            
                    
                    14.4：Flutter从启动到显示
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.5" data-path="../chapter14/image_and_cache.html">
            
                <a href="../chapter14/image_and_cache.html">
            
                    
                    14.5：Flutter图片加载与缓存
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">实例篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../chapter15/intro.html">
            
                <a href="../chapter15/intro.html">
            
                    
                    第十五章：一个完整的Flutter应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../chapter15/intro.html">
            
                <a href="../chapter15/intro.html">
            
                    
                    15.1：应用简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../chapter15/code_structure.html">
            
                <a href="../chapter15/code_structure.html">
            
                    
                    15.2：APP代码结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../chapter15/models.html">
            
                <a href="../chapter15/models.html">
            
                    
                    15.3：Model类定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="../chapter15/globals.html">
            
                <a href="../chapter15/globals.html">
            
                    
                    15.4：全局变量及共享状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="../chapter15/network.html">
            
                <a href="../chapter15/network.html">
            
                    
                    15.5：网络请求封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="../chapter15/entry.html">
            
                <a href="../chapter15/entry.html">
            
                    
                    15.6：App入口及首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="../chapter15/login_page.html">
            
                <a href="../chapter15/login_page.html">
            
                    
                    15.7：登录页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="../chapter15/language_and_theme_setting.html">
            
                <a href="../chapter15/language_and_theme_setting.html">
            
                    
                    15.8：多语言和多主题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="https://pcdn.flutterchina.club/gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    
    <a class="btn pull-right" aria-label="GitHub">
        <i class="fa fa-edit" title="编辑本文"></i>
    </a>
    <a class="btn pull-right" aria-label="GitHub" href="https://github.com/flutterchina/flutter-in-action">
        <i class="fa fa-github" title="查看本书开源项目"></i>
    </a>
    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >2.2：路由管理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="22-&#x8DEF;&#x7531;&#x7BA1;&#x7406;">2.2 &#x8DEF;&#x7531;&#x7BA1;&#x7406;</h1>
<p>&#x8DEF;&#x7531;(Route)&#x5728;&#x79FB;&#x52A8;&#x5F00;&#x53D1;&#x4E2D;&#x901A;&#x5E38;&#x6307;&#x9875;&#x9762;&#xFF08;Page&#xFF09;&#xFF0C;&#x8FD9;&#x8DDF;web&#x5F00;&#x53D1;&#x4E2D;&#x5355;&#x9875;&#x5E94;&#x7528;&#x7684;Route&#x6982;&#x5FF5;&#x610F;&#x4E49;&#x662F;&#x76F8;&#x540C;&#x7684;&#xFF0C;Route&#x5728;Android&#x4E2D;&#x901A;&#x5E38;&#x6307;&#x4E00;&#x4E2A;Activity&#xFF0C;&#x5728;iOS&#x4E2D;&#x6307;&#x4E00;&#x4E2A;ViewController&#x3002;&#x6240;&#x8C13;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#xFF0C;&#x5C31;&#x662F;&#x7BA1;&#x7406;&#x9875;&#x9762;&#x4E4B;&#x95F4;&#x5982;&#x4F55;&#x8DF3;&#x8F6C;&#xFF0C;&#x901A;&#x5E38;&#x4E5F;&#x53EF;&#x88AB;&#x79F0;&#x4E3A;&#x5BFC;&#x822A;&#x7BA1;&#x7406;&#x3002;Flutter&#x4E2D;&#x7684;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x548C;&#x539F;&#x751F;&#x5F00;&#x53D1;&#x7C7B;&#x4F3C;&#xFF0C;&#x65E0;&#x8BBA;&#x662F;Android&#x8FD8;&#x662F;iOS&#xFF0C;&#x5BFC;&#x822A;&#x7BA1;&#x7406;&#x90FD;&#x4F1A;&#x7EF4;&#x62A4;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x6808;&#xFF0C;&#x8DEF;&#x7531;&#x5165;&#x6808;(push)&#x64CD;&#x4F5C;&#x5BF9;&#x5E94;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x65B0;&#x9875;&#x9762;&#xFF0C;&#x8DEF;&#x7531;&#x51FA;&#x6808;(pop)&#x64CD;&#x4F5C;&#x5BF9;&#x5E94;&#x9875;&#x9762;&#x5173;&#x95ED;&#x64CD;&#x4F5C;&#xFF0C;&#x800C;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x4E3B;&#x8981;&#x662F;&#x6307;&#x5982;&#x4F55;&#x6765;&#x7BA1;&#x7406;&#x8DEF;&#x7531;&#x6808;&#x3002;</p>
<h2 id="221-&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x793A;&#x4F8B;">2.2.1 &#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x793A;&#x4F8B;</h2>
<p>&#x6211;&#x4EEC;&#x5728;&#x4E0A;&#x4E00;&#x8282;&#x201C;&#x8BA1;&#x6570;&#x5668;&#x201D;&#x793A;&#x4F8B;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#xFF0C;&#x505A;&#x5982;&#x4E0B;&#x4FEE;&#x6539;&#xFF1A;</p>
<ol>
<li><p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x8DEF;&#x7531;&#xFF0C;&#x547D;&#x540D;&#x201C;NewRoute&#x201D;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">NewRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Scaffold</span><span class="token punctuation">(</span>
      appBar<span class="token punctuation">:</span> <span class="token function">AppBar</span><span class="token punctuation">(</span>
        title<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;New route&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
      body<span class="token punctuation">:</span> <span class="token function">Center</span><span class="token punctuation">(</span>
        child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;This is new route&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x65B0;&#x8DEF;&#x7531;&#x7EE7;&#x627F;&#x81EA;<code>StatelessWidget</code>&#xFF0C;&#x754C;&#x9762;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x5728;&#x9875;&#x9762;&#x4E2D;&#x95F4;&#x663E;&#x793A;&#x4E00;&#x53E5;&quot;This is new route&quot;&#x3002;</p>
</li>
<li><p>&#x5728;<code>_MyHomePageState.build</code>&#x65B9;&#x6CD5;&#x4E2D;&#x7684;<code>Column</code>&#x7684;&#x5B50;widget&#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x6309;&#x94AE;&#xFF08;<code>FlatButton</code>&#xFF09; :</p>
<pre class="language-"><code class="lang-dart"><span class="token function">Column</span><span class="token punctuation">(</span>
      mainAxisAlignment<span class="token punctuation">:</span> MainAxisAlignment<span class="token punctuation">.</span>center<span class="token punctuation">,</span>
      children<span class="token punctuation">:</span> <span class="token operator">&lt;</span>Widget<span class="token operator">&gt;</span><span class="token punctuation">[</span>
      <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">//&#x7701;&#x7565;&#x65E0;&#x5173;&#x4EE3;&#x7801;</span>
      <span class="token function">FlatButton</span><span class="token punctuation">(</span>
         child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;open new route&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
         textColor<span class="token punctuation">:</span> Colors<span class="token punctuation">.</span>blue<span class="token punctuation">,</span>
         onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token comment">//&#x5BFC;&#x822A;&#x5230;&#x65B0;&#x8DEF;&#x7531;   </span>
          Navigator<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span> context<span class="token punctuation">,</span>
           <span class="token function">MaterialPageRoute</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token keyword">return</span> <span class="token function">NewRoute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
         <span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token punctuation">]</span><span class="token punctuation">,</span>
 <span class="token punctuation">)</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x6253;&#x5F00;&#x65B0;&#x8DEF;&#x7531;&#x7684;&#x6309;&#x94AE;&#xFF0C;&#x5E76;&#x5C06;&#x6309;&#x94AE;&#x6587;&#x5B57;&#x989C;&#x8272;&#x8BBE;&#x7F6E;&#x4E3A;&#x84DD;&#x8272;&#xFF0C;&#x70B9;&#x51FB;&#x8BE5;&#x6309;&#x94AE;&#x540E;&#x5C31;&#x4F1A;&#x6253;&#x5F00;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#x9875;&#x9762;&#xFF0C;&#x6548;&#x679C;&#x5982;&#x56FE;2-2&#x548C;2-3&#x6240;&#x793A;&#x3002;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/2-2.png" alt="&#x56FE;2-2"> <img src="https://pcdn.flutterchina.club/imgs/2-3.png" alt="&#x56FE;2-3"></p>
</li>
</ol>
<h2 id="222-materialpageroute">2.2.2 MaterialPageRoute</h2>
<p><code>MaterialPageRoute</code>&#x7EE7;&#x627F;&#x81EA;<code>PageRoute</code>&#x7C7B;&#xFF0C;<code>PageRoute</code>&#x7C7B;&#x662F;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;&#x7C7B;&#xFF0C;&#x8868;&#x793A;&#x5360;&#x6709;&#x6574;&#x4E2A;&#x5C4F;&#x5E55;&#x7A7A;&#x95F4;&#x7684;&#x4E00;&#x4E2A;&#x6A21;&#x6001;&#x8DEF;&#x7531;&#x9875;&#x9762;&#xFF0C;&#x5B83;&#x8FD8;&#x5B9A;&#x4E49;&#x4E86;&#x8DEF;&#x7531;&#x6784;&#x5EFA;&#x53CA;&#x5207;&#x6362;&#x65F6;&#x8FC7;&#x6E21;&#x52A8;&#x753B;&#x7684;&#x76F8;&#x5173;&#x63A5;&#x53E3;&#x53CA;&#x5C5E;&#x6027;&#x3002;<code>MaterialPageRoute</code> &#x662F;Material&#x7EC4;&#x4EF6;&#x5E93;&#x63D0;&#x4F9B;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x9488;&#x5BF9;&#x4E0D;&#x540C;&#x5E73;&#x53F0;&#xFF0C;&#x5B9E;&#x73B0;&#x4E0E;&#x5E73;&#x53F0;&#x9875;&#x9762;&#x5207;&#x6362;&#x52A8;&#x753B;&#x98CE;&#x683C;&#x4E00;&#x81F4;&#x7684;&#x8DEF;&#x7531;&#x5207;&#x6362;&#x52A8;&#x753B;&#xFF1A;</p>
<ul>
<li>&#x5BF9;&#x4E8E;Android&#xFF0C;&#x5F53;&#x6253;&#x5F00;&#x65B0;&#x9875;&#x9762;&#x65F6;&#xFF0C;&#x65B0;&#x7684;&#x9875;&#x9762;&#x4F1A;&#x4ECE;&#x5C4F;&#x5E55;&#x5E95;&#x90E8;&#x6ED1;&#x52A8;&#x5230;&#x5C4F;&#x5E55;&#x9876;&#x90E8;&#xFF1B;&#x5F53;&#x5173;&#x95ED;&#x9875;&#x9762;&#x65F6;&#xFF0C;&#x5F53;&#x524D;&#x9875;&#x9762;&#x4F1A;&#x4ECE;&#x5C4F;&#x5E55;&#x9876;&#x90E8;&#x6ED1;&#x52A8;&#x5230;&#x5C4F;&#x5E55;&#x5E95;&#x90E8;&#x540E;&#x6D88;&#x5931;&#xFF0C;&#x540C;&#x65F6;&#x4E0A;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x4F1A;&#x663E;&#x793A;&#x5230;&#x5C4F;&#x5E55;&#x4E0A;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;iOS&#xFF0C;&#x5F53;&#x6253;&#x5F00;&#x9875;&#x9762;&#x65F6;&#xFF0C;&#x65B0;&#x7684;&#x9875;&#x9762;&#x4F1A;&#x4ECE;&#x5C4F;&#x5E55;&#x53F3;&#x4FA7;&#x8FB9;&#x7F18;&#x4E00;&#x81F4;&#x6ED1;&#x52A8;&#x5230;&#x5C4F;&#x5E55;&#x5DE6;&#x8FB9;&#xFF0C;&#x76F4;&#x5230;&#x65B0;&#x9875;&#x9762;&#x5168;&#x90E8;&#x663E;&#x793A;&#x5230;&#x5C4F;&#x5E55;&#x4E0A;&#xFF0C;&#x800C;&#x4E0A;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x5219;&#x4F1A;&#x4ECE;&#x5F53;&#x524D;&#x5C4F;&#x5E55;&#x6ED1;&#x52A8;&#x5230;&#x5C4F;&#x5E55;&#x5DE6;&#x4FA7;&#x800C;&#x6D88;&#x5931;&#xFF1B;&#x5F53;&#x5173;&#x95ED;&#x9875;&#x9762;&#x65F6;&#xFF0C;&#x6B63;&#x597D;&#x76F8;&#x53CD;&#xFF0C;&#x5F53;&#x524D;&#x9875;&#x9762;&#x4F1A;&#x4ECE;&#x5C4F;&#x5E55;&#x53F3;&#x4FA7;&#x6ED1;&#x51FA;&#xFF0C;&#x540C;&#x65F6;&#x4E0A;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x4F1A;&#x4ECE;&#x5C4F;&#x5E55;&#x5DE6;&#x4FA7;&#x6ED1;&#x5165;&#x3002;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;<code>MaterialPageRoute</code> &#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5404;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x610F;&#x4E49;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">  <span class="token function">MaterialPageRoute</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    WidgetBuilder builder<span class="token punctuation">,</span>
    RouteSettings settings<span class="token punctuation">,</span>
    bool maintainState <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    bool fullscreenDialog <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<ul>
<li><code>builder</code> &#x662F;&#x4E00;&#x4E2A;WidgetBuilder&#x7C7B;&#x578B;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x6784;&#x5EFA;&#x8DEF;&#x7531;&#x9875;&#x9762;&#x7684;&#x5177;&#x4F53;&#x5185;&#x5BB9;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;widget&#x3002;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x8981;&#x5B9E;&#x73B0;&#x6B64;&#x56DE;&#x8C03;&#xFF0C;&#x8FD4;&#x56DE;&#x65B0;&#x8DEF;&#x7531;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</li>
<li><code>settings</code> &#x5305;&#x542B;&#x8DEF;&#x7531;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#x8DEF;&#x7531;&#x540D;&#x79F0;&#x3001;&#x662F;&#x5426;&#x521D;&#x59CB;&#x8DEF;&#x7531;&#xFF08;&#x9996;&#x9875;&#xFF09;&#x3002;</li>
<li><code>maintainState</code>&#xFF1A;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5F53;&#x5165;&#x6808;&#x4E00;&#x4E2A;&#x65B0;&#x8DEF;&#x7531;&#x65F6;&#xFF0C;&#x539F;&#x6765;&#x7684;&#x8DEF;&#x7531;&#x4ECD;&#x7136;&#x4F1A;&#x88AB;&#x4FDD;&#x5B58;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x5728;&#x8DEF;&#x7531;&#x6CA1;&#x7528;&#x7684;&#x65F6;&#x5019;&#x91CA;&#x653E;&#x5176;&#x6240;&#x5360;&#x7528;&#x7684;&#x6240;&#x6709;&#x8D44;&#x6E90;&#xFF0C;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;<code>maintainState</code>&#x4E3A;false&#x3002;</li>
<li><code>fullscreenDialog</code>&#x8868;&#x793A;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#x9875;&#x9762;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A;&#x5168;&#x5C4F;&#x7684;&#x6A21;&#x6001;&#x5BF9;&#x8BDD;&#x6846;&#xFF0C;&#x5728;iOS&#x4E2D;&#xFF0C;&#x5982;&#x679C;<code>fullscreenDialog</code>&#x4E3A;<code>true</code>&#xFF0C;&#x65B0;&#x9875;&#x9762;&#x5C06;&#x4F1A;&#x4ECE;&#x5C4F;&#x5E55;&#x5E95;&#x90E8;&#x6ED1;&#x5165;&#xFF08;&#x800C;&#x4E0D;&#x662F;&#x6C34;&#x5E73;&#x65B9;&#x5411;&#xFF09;&#x3002;</li>
</ul>
<blockquote>
<p>&#x5982;&#x679C;&#x60F3;&#x81EA;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x5207;&#x6362;&#x52A8;&#x753B;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x7EE7;&#x627F;PageRoute&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x540E;&#x9762;&#x4ECB;&#x7ECD;&#x52A8;&#x753B;&#x65F6;&#xFF0C;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x8DEF;&#x7531;&#x7EC4;&#x4EF6;&#x3002;</p>
</blockquote>
<h2 id="223-navigator">2.2.3 Navigator</h2>
<p><code>Navigator</code>&#x662F;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x6253;&#x5F00;&#x548C;&#x9000;&#x51FA;&#x8DEF;&#x7531;&#x9875;&#x65B9;&#x6CD5;&#x3002;<code>Navigator</code>&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x6808;&#x6765;&#x7BA1;&#x7406;&#x6D3B;&#x52A8;&#x8DEF;&#x7531;&#x96C6;&#x5408;&#x3002;&#x901A;&#x5E38;&#x5F53;&#x524D;&#x5C4F;&#x5E55;&#x663E;&#x793A;&#x7684;&#x9875;&#x9762;&#x5C31;&#x662F;&#x6808;&#x9876;&#x7684;&#x8DEF;&#x7531;&#x3002;<code>Navigator</code>&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x65B9;&#x6CD5;&#x6765;&#x7BA1;&#x7406;&#x8DEF;&#x7531;&#x6808;&#xFF0C;&#x5728;&#x6B64;&#x6211;&#x4EEC;&#x53EA;&#x4ECB;&#x7ECD;&#x5176;&#x6700;&#x5E38;&#x7528;&#x7684;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<h3 id="future--pushbuildcontext-context-route-route">Future  push(BuildContext context, Route route)</h3>
<p>&#x5C06;&#x7ED9;&#x5B9A;&#x7684;&#x8DEF;&#x7531;&#x5165;&#x6808;&#xFF08;&#x5373;&#x6253;&#x5F00;&#x65B0;&#x7684;&#x9875;&#x9762;&#xFF09;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;<code>Future</code>&#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x4EE5;&#x63A5;&#x6536;&#x65B0;&#x8DEF;&#x7531;&#x51FA;&#x6808;&#xFF08;&#x5373;&#x5173;&#x95ED;&#xFF09;&#x65F6;&#x7684;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x3002;</p>
<h3 id="bool--popbuildcontext-context--result-">bool  pop(BuildContext context, [ result ])</h3>
<p>&#x5C06;&#x6808;&#x9876;&#x8DEF;&#x7531;&#x51FA;&#x6808;&#xFF0C;<code>result</code>&#x4E3A;&#x9875;&#x9762;&#x5173;&#x95ED;&#x65F6;&#x8FD4;&#x56DE;&#x7ED9;&#x4E0A;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<p><code>Navigator</code> &#x8FD8;&#x6709;&#x5F88;&#x591A;&#x5176;&#x5B83;&#x65B9;&#x6CD5;&#xFF0C;&#x5982;<code>Navigator.replace</code>&#x3001;<code>Navigator.popUntil</code>&#x7B49;&#xFF0C;&#x8BE6;&#x60C5;&#x8BF7;&#x53C2;&#x8003;API&#x6587;&#x6863;&#x6216;SDK&#x6E90;&#x7801;&#x6CE8;&#x91CA;&#xFF0C;&#x5728;&#x6B64;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x8DEF;&#x7531;&#x76F8;&#x5173;&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x6982;&#x5FF5;&#x201C;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x201D;&#x3002;</p>
<h3 id="&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;">&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</h3>
<p>Navigator&#x7C7B;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;context&#x7684;<strong>&#x9759;&#x6001;&#x65B9;&#x6CD5;</strong>&#x90FD;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;Navigator&#x7684;<strong>&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</strong>&#xFF0C; &#x6BD4;&#x5982;<code>Navigator.push(BuildContext context, Route route)</code>&#x7B49;&#x4EF7;&#x4E8E;<code>Navigator.of(context).push(Route route)</code> &#xFF0C;&#x4E0B;&#x9762;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x76F8;&#x5173;&#x7684;&#x65B9;&#x6CD5;&#x4E5F;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>
<h2 id="224-&#x8DEF;&#x7531;&#x4F20;&#x503C;">2.2.4 &#x8DEF;&#x7531;&#x4F20;&#x503C;</h2>
<p>&#x5F88;&#x591A;&#x65F6;&#x5019;&#xFF0C;&#x5728;&#x8DEF;&#x7531;&#x8DF3;&#x8F6C;&#x65F6;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5E26;&#x4E00;&#x4E9B;&#x53C2;&#x6570;&#xFF0C;&#x6BD4;&#x5982;&#x6253;&#x5F00;&#x5546;&#x54C1;&#x8BE6;&#x60C5;&#x9875;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5E26;&#x4E00;&#x4E2A;&#x5546;&#x54C1;id&#xFF0C;&#x8FD9;&#x6837;&#x5546;&#x54C1;&#x8BE6;&#x60C5;&#x9875;&#x624D;&#x77E5;&#x9053;&#x5C55;&#x793A;&#x54EA;&#x4E2A;&#x5546;&#x54C1;&#x4FE1;&#x606F;&#xFF1B;&#x53C8;&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x5728;&#x586B;&#x5199;&#x8BA2;&#x5355;&#x65F6;&#x9700;&#x8981;&#x9009;&#x62E9;&#x6536;&#x8D27;&#x5730;&#x5740;&#xFF0C;&#x6253;&#x5F00;&#x5730;&#x5740;&#x9009;&#x62E9;&#x9875;&#x5E76;&#x9009;&#x62E9;&#x5730;&#x5740;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x7528;&#x6237;&#x9009;&#x62E9;&#x7684;&#x5730;&#x5740;&#x8FD4;&#x56DE;&#x5230;&#x8BA2;&#x5355;&#x9875;&#x7B49;&#x7B49;&#x3002;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x793A;&#x4F8B;&#x6765;&#x6F14;&#x793A;&#x65B0;&#x65E7;&#x8DEF;&#x7531;&#x5982;&#x4F55;&#x4F20;&#x53C2;&#x3002;</p>
<h3 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h3>
<p>&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;<code>TipRoute</code>&#x8DEF;&#x7531;&#xFF0C;&#x5B83;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x63D0;&#x793A;&#x6587;&#x672C;&#x53C2;&#x6570;&#xFF0C;&#x8D1F;&#x8D23;&#x5C06;&#x4F20;&#x5165;&#x5B83;&#x7684;&#x6587;&#x672C;&#x663E;&#x793A;&#x5728;&#x9875;&#x9762;&#x4E0A;&#xFF0C;&#x53E6;&#x5916;<code>TipRoute</code>&#x4E2D;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x201C;&#x8FD4;&#x56DE;&#x201D;&#x6309;&#x94AE;&#xFF0C;&#x70B9;&#x51FB;&#x540E;&#x5728;&#x8FD4;&#x56DE;&#x4E0A;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x7684;&#x540C;&#x65F6;&#x4F1A;&#x5E26;&#x4E0A;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x53C2;&#x6570;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B;&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x3002;</p>
<p><code>TipRoute</code>&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">TipRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token function">TipRoute</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    Key key<span class="token punctuation">,</span>
    <span class="token metadata symbol">@required</span> <span class="token keyword">this</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span>  <span class="token comment">// &#x63A5;&#x6536;&#x4E00;&#x4E2A;text&#x53C2;&#x6570;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword">super</span><span class="token punctuation">(</span>key<span class="token punctuation">:</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">final</span> String text<span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Scaffold</span><span class="token punctuation">(</span>
      appBar<span class="token punctuation">:</span> <span class="token function">AppBar</span><span class="token punctuation">(</span>
        title<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x63D0;&#x793A;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
      body<span class="token punctuation">:</span> <span class="token function">Padding</span><span class="token punctuation">(</span>
        padding<span class="token punctuation">:</span> EdgeInsets<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        child<span class="token punctuation">:</span> <span class="token function">Center</span><span class="token punctuation">(</span>
          child<span class="token punctuation">:</span> <span class="token function">Column</span><span class="token punctuation">(</span>
            children<span class="token punctuation">:</span> <span class="token operator">&lt;</span>Widget<span class="token operator">&gt;</span><span class="token punctuation">[</span>
              <span class="token function">Text</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">,</span>
              <span class="token function">RaisedButton</span><span class="token punctuation">(</span>
                onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> Navigator<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token string">&quot;&#x6211;&#x662F;&#x8FD4;&#x56DE;&#x503C;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x8FD4;&#x56DE;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
              <span class="token punctuation">)</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
          <span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x6253;&#x5F00;&#x65B0;&#x8DEF;&#x7531;<code>TipRoute</code>&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">RouterTestRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Center</span><span class="token punctuation">(</span>
      child<span class="token punctuation">:</span> <span class="token function">RaisedButton</span><span class="token punctuation">(</span>
        onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">async</span> <span class="token punctuation">{</span>
          <span class="token comment">// &#x6253;&#x5F00;`TipRoute`&#xFF0C;&#x5E76;&#x7B49;&#x5F85;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;</span>
          <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token keyword">await</span> Navigator<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
            context<span class="token punctuation">,</span>
            <span class="token function">MaterialPageRoute</span><span class="token punctuation">(</span>
              builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token function">TipRoute</span><span class="token punctuation">(</span>
                  <span class="token comment">// &#x8DEF;&#x7531;&#x53C2;&#x6570;</span>
                  text<span class="token punctuation">:</span> <span class="token string">&quot;&#x6211;&#x662F;&#x63D0;&#x793A;xxxx&quot;</span><span class="token punctuation">,</span>
                <span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">)</span><span class="token punctuation">,</span>
          <span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token comment">//&#x8F93;&#x51FA;`TipRoute`&#x8DEF;&#x7531;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;</span>
          <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;&#x8DEF;&#x7531;&#x8FD4;&#x56DE;&#x503C;: $result&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6253;&#x5F00;&#x63D0;&#x793A;&#x9875;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#xFF0C;&#x70B9;&#x51FB;<code>RouterTestRoute</code>&#x9875;&#x7684;&#x201C;&#x6253;&#x5F00;&#x63D0;&#x793A;&#x9875;&#x201D;&#x6309;&#x94AE;&#xFF0C;&#x4F1A;&#x6253;&#x5F00;<code>TipRoute</code>&#x9875;&#xFF0C;&#x8FD0;&#x884C;&#x6548;&#x679C;&#x5982;&#x56FE;2-4&#x6240;&#x793A;&#x4E0B;&#xFF1A;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/2-4.png" alt="&#x56FE;2-4"></p>
<p>&#x9700;&#x8981;&#x8BF4;&#x660E;&#xFF1A;</p>
<ol>
<li><p>&#x63D0;&#x793A;&#x6587;&#x6848;&#x201C;&#x6211;&#x662F;&#x63D0;&#x793A;xxxx&#x201D;&#x662F;&#x901A;&#x8FC7;<code>TipRoute</code>&#x7684;<code>text</code>&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x65B0;&#x8DEF;&#x7531;&#x9875;&#x7684;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7B49;&#x5F85;<code>Navigator.push(&#x2026;)</code>&#x8FD4;&#x56DE;&#x7684;<code>Future</code>&#x6765;&#x83B7;&#x53D6;&#x65B0;&#x8DEF;&#x7531;&#x7684;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x3002;</p>
</li>
<li><p>&#x5728;<code>TipRoute</code>&#x9875;&#x4E2D;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x5230;&#x4E0A;&#x4E00;&#x9875;&#xFF1B;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x65F6;&#x76F4;&#x63A5;&#x70B9;&#x51FB;&#x5BFC;&#x822A;&#x680F;&#x8FD4;&#x56DE;&#x7BAD;&#x5934;&#xFF0C;&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x70B9;&#x51FB;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x201C;&#x8FD4;&#x56DE;&#x201D;&#x6309;&#x94AE;&#x3002;&#x8FD9;&#x4E24;&#x79CD;&#x8FD4;&#x56DE;&#x65B9;&#x5F0F;&#x7684;&#x533A;&#x522B;&#x662F;&#x524D;&#x8005;&#x4E0D;&#x4F1A;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x7ED9;&#x4E0A;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#xFF0C;&#x800C;&#x540E;&#x8005;&#x4F1A;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x5206;&#x522B;&#x70B9;&#x51FB;&#x9875;&#x9762;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x6309;&#x94AE;&#x548C;&#x5BFC;&#x822A;&#x680F;&#x8FD4;&#x56DE;&#x7BAD;&#x5934;&#x540E;&#xFF0C;<code>RouterTestRoute</code>&#x9875;&#x4E2D;<code>print</code>&#x65B9;&#x6CD5;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x8F93;&#x51FA;&#x7684;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre class="language-"><code>I/flutter (27896): &#x8DEF;&#x7531;&#x8FD4;&#x56DE;&#x503C;: &#x6211;&#x662F;&#x8FD4;&#x56DE;&#x503C;
I/flutter (27896): &#x8DEF;&#x7531;&#x8FD4;&#x56DE;&#x503C;: null
</code></pre></li>
</ol>
<p>&#x4E0A;&#x9762;&#x4ECB;&#x7ECD;&#x7684;&#x662F;&#x975E;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x7684;&#x4F20;&#x503C;&#x65B9;&#x5F0F;&#xFF0C;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x7684;&#x4F20;&#x503C;&#x65B9;&#x5F0F;&#x4F1A;&#x6709;&#x6240;&#x4E0D;&#x540C;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x4E0B;&#x9762;&#x4ECB;&#x7ECD;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x65F6;&#x4ECB;&#x7ECD;&#x3002;</p>
<h2 id="225-&#x547D;&#x540D;&#x8DEF;&#x7531;">2.2.5 &#x547D;&#x540D;&#x8DEF;&#x7531;</h2>
<p>&#x6240;&#x8C13;&#x201C;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x201D;&#xFF08;Named Route&#xFF09;&#x5373;&#x6709;&#x540D;&#x5B57;&#x7684;&#x8DEF;&#x7531;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5148;&#x7ED9;&#x8DEF;&#x7531;&#x8D77;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#xFF0C;&#x7136;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x5B57;&#x76F4;&#x63A5;&#x6253;&#x5F00;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#x4E86;&#xFF0C;&#x8FD9;&#x4E3A;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x5E26;&#x6765;&#x4E86;&#x4E00;&#x79CD;&#x76F4;&#x89C2;&#x3001;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x5F0F;&#x3002;</p>
<h3 id="&#x8DEF;&#x7531;&#x8868;">&#x8DEF;&#x7531;&#x8868;</h3>
<p>&#x8981;&#x60F3;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x8DEF;&#x7531;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x5148;&#x63D0;&#x4F9B;&#x5E76;&#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x8868;&#xFF08;routing table&#xFF09;&#xFF0C;&#x8FD9;&#x6837;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x624D;&#x77E5;&#x9053;&#x54EA;&#x4E2A;&#x540D;&#x5B57;&#x4E0E;&#x54EA;&#x4E2A;&#x8DEF;&#x7531;&#x7EC4;&#x4EF6;&#x76F8;&#x5BF9;&#x5E94;&#x3002;&#x5176;&#x5B9E;&#x6CE8;&#x518C;&#x8DEF;&#x7531;&#x8868;&#x5C31;&#x662F;&#x7ED9;&#x8DEF;&#x7531;&#x8D77;&#x540D;&#x5B57;&#xFF0C;&#x8DEF;&#x7531;&#x8868;&#x7684;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> WidgetBuilder<span class="token operator">&gt;</span> routes<span class="token punctuation">;</span>
</code></pre>
<p>&#x5B83;&#x662F;&#x4E00;&#x4E2A;<code>Map</code>&#xFF0C;key&#x4E3A;&#x8DEF;&#x7531;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x662F;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF1B;value&#x662F;&#x4E2A;<code>builder</code>&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x7528;&#x4E8E;&#x751F;&#x6210;&#x76F8;&#x5E94;&#x7684;&#x8DEF;&#x7531;widget&#x3002;&#x6211;&#x4EEC;&#x5728;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x5B57;&#x6253;&#x5F00;&#x65B0;&#x8DEF;&#x7531;&#x65F6;&#xFF0C;&#x5E94;&#x7528;&#x4F1A;&#x6839;&#x636E;&#x8DEF;&#x7531;&#x540D;&#x5B57;&#x5728;&#x8DEF;&#x7531;&#x8868;&#x4E2D;&#x67E5;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684;<code>WidgetBuilder</code>&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;&#x8BE5;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x751F;&#x6210;&#x8DEF;&#x7531;widget&#x5E76;&#x8FD4;&#x56DE;&#x3002;</p>
<h3 id="&#x6CE8;&#x518C;&#x8DEF;&#x7531;&#x8868;">&#x6CE8;&#x518C;&#x8DEF;&#x7531;&#x8868;</h3>
<p>&#x8DEF;&#x7531;&#x8868;&#x7684;&#x6CE8;&#x518C;&#x65B9;&#x5F0F;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x56DE;&#x5230;&#x4E4B;&#x524D;&#x201C;&#x8BA1;&#x6570;&#x5668;&#x201D;&#x7684;&#x793A;&#x4F8B;&#xFF0C;&#x7136;&#x540E;&#x5728;<code>MyApp</code>&#x7C7B;&#x7684;<code>build</code>&#x65B9;&#x6CD5;&#x4E2D;&#x627E;&#x5230;<code>MaterialApp</code>&#xFF0C;&#x6DFB;&#x52A0;<code>routes</code>&#x5C5E;&#x6027;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">MaterialApp</span><span class="token punctuation">(</span>
  title<span class="token punctuation">:</span> <span class="token string">&apos;Flutter Demo&apos;</span><span class="token punctuation">,</span>
  theme<span class="token punctuation">:</span> <span class="token function">ThemeData</span><span class="token punctuation">(</span>
    primarySwatch<span class="token punctuation">:</span> Colors<span class="token punctuation">.</span>blue<span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token comment">//&#x6CE8;&#x518C;&#x8DEF;&#x7531;&#x8868;</span>
  routes<span class="token punctuation">:</span><span class="token punctuation">{</span>
   <span class="token string">&quot;new_page&quot;</span><span class="token punctuation">:</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">NewRoute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">// &#x7701;&#x7565;&#x5176;&#x5B83;&#x8DEF;&#x7531;&#x6CE8;&#x518C;&#x4FE1;&#x606F;</span>
  <span class="token punctuation">}</span> <span class="token punctuation">,</span>
  home<span class="token punctuation">:</span> <span class="token function">MyHomePage</span><span class="token punctuation">(</span>title<span class="token punctuation">:</span> <span class="token string">&apos;Flutter Demo Home Page&apos;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5C31;&#x5B8C;&#x6210;&#x4E86;&#x8DEF;&#x7531;&#x8868;&#x7684;&#x6CE8;&#x518C;&#x3002;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E2D;<code>home</code>&#x8DEF;&#x7531;&#x5E76;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x8DEF;&#x7531;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4E5F;&#x60F3;&#x5C06;<code>home</code>&#x6CE8;&#x518C;&#x4E3A;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x5E94;&#x8BE5;&#x600E;&#x4E48;&#x505A;&#x5462;&#xFF1F;&#x5176;&#x5B9E;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x76F4;&#x63A5;&#x770B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">MaterialApp</span><span class="token punctuation">(</span>
  title<span class="token punctuation">:</span> <span class="token string">&apos;Flutter Demo&apos;</span><span class="token punctuation">,</span>
  initialRoute<span class="token punctuation">:</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> <span class="token comment">//&#x540D;&#x4E3A;&quot;/&quot;&#x7684;&#x8DEF;&#x7531;&#x4F5C;&#x4E3A;&#x5E94;&#x7528;&#x7684;home(&#x9996;&#x9875;)</span>
  theme<span class="token punctuation">:</span> <span class="token function">ThemeData</span><span class="token punctuation">(</span>
    primarySwatch<span class="token punctuation">:</span> Colors<span class="token punctuation">.</span>blue<span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token comment">//&#x6CE8;&#x518C;&#x8DEF;&#x7531;&#x8868;</span>
  routes<span class="token punctuation">:</span><span class="token punctuation">{</span>
   <span class="token string">&quot;new_page&quot;</span><span class="token punctuation">:</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">NewRoute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
   <span class="token string">&quot;/&quot;</span><span class="token punctuation">:</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">MyHomePage</span><span class="token punctuation">(</span>title<span class="token punctuation">:</span> <span class="token string">&apos;Flutter Demo Home Page&apos;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//&#x6CE8;&#x518C;&#x9996;&#x9875;&#x8DEF;&#x7531;</span>
  <span class="token punctuation">}</span> 
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x5728;&#x8DEF;&#x7531;&#x8868;&#x4E2D;&#x6CE8;&#x518C;&#x4E00;&#x4E0B;<code>MyHomePage</code>&#x8DEF;&#x7531;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x5176;&#x540D;&#x5B57;&#x4F5C;&#x4E3A;<code>MaterialApp</code>&#x7684;<code>initialRoute</code>&#x5C5E;&#x6027;&#x503C;&#x5373;&#x53EF;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x51B3;&#x5B9A;&#x5E94;&#x7528;&#x7684;&#x521D;&#x59CB;&#x8DEF;&#x7531;&#x9875;&#x662F;&#x54EA;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x3002;</p>
<h3 id="&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x6253;&#x5F00;&#x65B0;&#x8DEF;&#x7531;&#x9875;">&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x6253;&#x5F00;&#x65B0;&#x8DEF;&#x7531;&#x9875;</h3>
<p>&#x8981;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x79F0;&#x6765;&#x6253;&#x5F00;&#x65B0;&#x8DEF;&#x7531;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>Navigator</code> &#x7684;<code>pushNamed</code>&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Future <span class="token function">pushNamed</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">,</span> String routeName<span class="token punctuation">,</span><span class="token punctuation">{</span>Object arguments<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p><code>Navigator</code> &#x9664;&#x4E86;<code>pushNamed</code>&#x65B9;&#x6CD5;&#xFF0C;&#x8FD8;&#x6709;<code>pushReplacementNamed</code>&#x7B49;&#x5176;&#x4ED6;&#x7BA1;&#x7406;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x81EA;&#x884C;&#x67E5;&#x770B;API&#x6587;&#x6863;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x6765;&#x6253;&#x5F00;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#x9875;&#xFF0C;&#x4FEE;&#x6539;<code>FlatButton</code>&#x7684;<code>onPressed</code>&#x56DE;&#x8C03;&#x4EE3;&#x7801;&#xFF0C;&#x6539;&#x4E3A;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  Navigator<span class="token punctuation">.</span><span class="token function">pushNamed</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token string">&quot;new_page&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">//Navigator.push(context,</span>
  <span class="token comment">//  MaterialPageRoute(builder: (context) {</span>
  <span class="token comment">//  return NewRoute();</span>
  <span class="token comment">//}));  </span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre>
<p>&#x70ED;&#x91CD;&#x8F7D;&#x5E94;&#x7528;&#xFF0C;&#x518D;&#x6B21;&#x70B9;&#x51FB;&#x201C;open new route&#x201D;&#x6309;&#x94AE;&#xFF0C;&#x4F9D;&#x7136;&#x53EF;&#x4EE5;&#x6253;&#x5F00;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#x9875;&#x3002;</p>
<h3 id="&#x547D;&#x540D;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#x4F20;&#x9012;">&#x547D;&#x540D;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#x4F20;&#x9012;</h3>
<p>&#x5728;Flutter&#x6700;&#x521D;&#x7684;&#x7248;&#x672C;&#x4E2D;&#xFF0C;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x662F;&#x4E0D;&#x80FD;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x7684;&#xFF0C;&#x540E;&#x6765;&#x624D;&#x652F;&#x6301;&#x4E86;&#x53C2;&#x6570;&#xFF1B;&#x4E0B;&#x9762;&#x5C55;&#x793A;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x5982;&#x4F55;&#x4F20;&#x9012;&#x5E76;&#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#xFF1A;</p>
<p>&#x6211;&#x4EEC;&#x5148;&#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"> routes<span class="token punctuation">:</span><span class="token punctuation">{</span>
   <span class="token string">&quot;new_page&quot;</span><span class="token punctuation">:</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">EchoRoute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span> <span class="token punctuation">,</span>
</code></pre>
<p>&#x5728;&#x8DEF;&#x7531;&#x9875;&#x901A;&#x8FC7;<code>RouteSetting</code>&#x5BF9;&#x8C61;&#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">EchoRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>

  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x53C2;&#x6570;  </span>
    <span class="token keyword">var</span> args<span class="token operator">=</span>ModalRoute<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">.</span>settings<span class="token punctuation">.</span>arguments<span class="token punctuation">;</span>
    <span class="token comment">//...&#x7701;&#x7565;&#x65E0;&#x5173;&#x4EE3;&#x7801;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5728;&#x6253;&#x5F00;&#x8DEF;&#x7531;&#x65F6;&#x4F20;&#x9012;&#x53C2;&#x6570;</p>
<pre class="language-"><code class="lang-dart">Navigator<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pushNamed</span><span class="token punctuation">(</span><span class="token string">&quot;new_page&quot;</span><span class="token punctuation">,</span> arguments<span class="token punctuation">:</span> <span class="token string">&quot;hi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="&#x9002;&#x914D;">&#x9002;&#x914D;</h3>
<p>&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x4E5F;&#x60F3;&#x5C06;&#x4E0A;&#x9762;&#x8DEF;&#x7531;&#x4F20;&#x53C2;&#x793A;&#x4F8B;&#x4E2D;&#x7684;<code>TipRoute</code>&#x8DEF;&#x7531;&#x9875;&#x6CE8;&#x518C;&#x5230;&#x8DEF;&#x7531;&#x8868;&#x4E2D;&#xFF0C;&#x4EE5;&#x4FBF;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x540D;&#x6765;&#x6253;&#x5F00;&#x5B83;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x7531;&#x4E8E;<code>TipRoute</code>&#x63A5;&#x53D7;&#x4E00;&#x4E2A;<code>text</code> &#x53C2;&#x6570;&#xFF0C;&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x5728;&#x4E0D;&#x6539;&#x53D8;<code>TipRoute</code>&#x6E90;&#x7801;&#x7684;&#x524D;&#x63D0;&#x4E0B;&#x9002;&#x914D;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF1F;&#x5176;&#x5B9E;&#x5F88;&#x7B80;&#x5355;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">MaterialApp</span><span class="token punctuation">(</span>
  <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">//&#x7701;&#x7565;&#x65E0;&#x5173;&#x4EE3;&#x7801;</span>
  routes<span class="token punctuation">:</span> <span class="token punctuation">{</span>
   <span class="token string">&quot;tip2&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">{</span>
     <span class="token keyword">return</span> <span class="token function">TipRoute</span><span class="token punctuation">(</span>text<span class="token punctuation">:</span> ModalRoute<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">.</span>settings<span class="token punctuation">.</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span><span class="token punctuation">,</span>
 <span class="token punctuation">}</span><span class="token punctuation">,</span> 
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h2 id="226-&#x8DEF;&#x7531;&#x751F;&#x6210;&#x94A9;&#x5B50;">2.2.6 &#x8DEF;&#x7531;&#x751F;&#x6210;&#x94A9;&#x5B50;</h2>
<p>&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x8981;&#x5F00;&#x53D1;&#x4E00;&#x4E2A;&#x7535;&#x5546;APP&#xFF0C;&#x5F53;&#x7528;&#x6237;&#x6CA1;&#x6709;&#x767B;&#x5F55;&#x65F6;&#x53EF;&#x4EE5;&#x770B;&#x5E97;&#x94FA;&#x3001;&#x5546;&#x54C1;&#x7B49;&#x4FE1;&#x606F;&#xFF0C;&#x4F46;&#x4EA4;&#x6613;&#x8BB0;&#x5F55;&#x3001;&#x8D2D;&#x7269;&#x8F66;&#x3001;&#x7528;&#x6237;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;&#x7B49;&#x9875;&#x9762;&#x9700;&#x8981;&#x767B;&#x5F55;&#x540E;&#x624D;&#x80FD;&#x770B;&#x3002;&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x4E0A;&#x8FF0;&#x529F;&#x80FD;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x6253;&#x5F00;&#x6BCF;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x9875;&#x524D;&#x5224;&#x65AD;&#x7528;&#x6237;&#x767B;&#x5F55;&#x72B6;&#x6001;&#xFF01;&#x5982;&#x679C;&#x6BCF;&#x6B21;&#x6253;&#x5F00;&#x8DEF;&#x7531;&#x524D;&#x6211;&#x4EEC;&#x90FD;&#x9700;&#x8981;&#x53BB;&#x5224;&#x65AD;&#x4E00;&#x4E0B;&#x5C06;&#x4F1A;&#x975E;&#x5E38;&#x9EBB;&#x70E6;&#xFF0C;&#x90A3;&#x6709;&#x4EC0;&#x4E48;&#x66F4;&#x597D;&#x7684;&#x529E;&#x6CD5;&#x5417;&#xFF1F;&#x7B54;&#x6848;&#x662F;&#x6709;&#xFF01;</p>
<p><code>MaterialApp</code>&#x6709;&#x4E00;&#x4E2A;<code>onGenerateRoute</code>&#x5C5E;&#x6027;&#xFF0C;&#x5B83;&#x5728;&#x6253;&#x5F00;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x65F6;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4E4B;&#x6240;&#x4EE5;&#x8BF4;&#x53EF;&#x80FD;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x5F53;&#x8C03;&#x7528;<code>Navigator.pushNamed(...)</code>&#x6253;&#x5F00;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6307;&#x5B9A;&#x7684;&#x8DEF;&#x7531;&#x540D;&#x5728;&#x8DEF;&#x7531;&#x8868;&#x4E2D;&#x5DF2;&#x6CE8;&#x518C;&#xFF0C;&#x5219;&#x4F1A;&#x8C03;&#x7528;&#x8DEF;&#x7531;&#x8868;&#x4E2D;&#x7684;<code>builder</code>&#x51FD;&#x6570;&#x6765;&#x751F;&#x6210;&#x8DEF;&#x7531;&#x7EC4;&#x4EF6;&#xFF1B;&#x5982;&#x679C;&#x8DEF;&#x7531;&#x8868;&#x4E2D;&#x6CA1;&#x6709;&#x6CE8;&#x518C;&#xFF0C;&#x624D;&#x4F1A;&#x8C03;&#x7528;<code>onGenerateRoute</code>&#x6765;&#x751F;&#x6210;&#x8DEF;&#x7531;&#x3002;<code>onGenerateRoute</code>&#x56DE;&#x8C03;&#x7B7E;&#x540D;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Route<span class="token operator">&lt;</span><span class="token keyword">dynamic</span><span class="token operator">&gt;</span> <span class="token function">Function</span><span class="token punctuation">(</span>RouteSettings settings<span class="token punctuation">)</span>
</code></pre>
<p>&#x6709;&#x4E86;<code>onGenerateRoute</code>&#x56DE;&#x8C03;&#xFF0C;&#x8981;&#x5B9E;&#x73B0;&#x4E0A;&#x9762;&#x63A7;&#x5236;&#x9875;&#x9762;&#x6743;&#x9650;&#x7684;&#x529F;&#x80FD;&#x5C31;&#x975E;&#x5E38;&#x5BB9;&#x6613;&#xFF1A;&#x6211;&#x4EEC;&#x653E;&#x5F03;&#x4F7F;&#x7528;&#x8DEF;&#x7531;&#x8868;&#xFF0C;&#x53D6;&#x800C;&#x4EE3;&#x4E4B;&#x7684;&#x662F;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;<code>onGenerateRoute</code>&#x56DE;&#x8C03;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x8BE5;&#x56DE;&#x8C03;&#x4E2D;&#x8FDB;&#x884C;&#x7EDF;&#x4E00;&#x7684;&#x6743;&#x9650;&#x63A7;&#x5236;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">MaterialApp</span><span class="token punctuation">(</span>
  <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">//&#x7701;&#x7565;&#x65E0;&#x5173;&#x4EE3;&#x7801;</span>
  onGenerateRoute<span class="token punctuation">:</span><span class="token punctuation">(</span>RouteSettings settings<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token function">MaterialPageRoute</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">{</span>
           String routeName <span class="token operator">=</span> settings<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
       <span class="token comment">// &#x5982;&#x679C;&#x8BBF;&#x95EE;&#x7684;&#x8DEF;&#x7531;&#x9875;&#x9700;&#x8981;&#x767B;&#x5F55;&#xFF0C;&#x4F46;&#x5F53;&#x524D;&#x672A;&#x767B;&#x5F55;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x767B;&#x5F55;&#x9875;&#x8DEF;&#x7531;&#xFF0C;</span>
       <span class="token comment">// &#x5F15;&#x5BFC;&#x7528;&#x6237;&#x767B;&#x5F55;&#xFF1B;&#x5176;&#x5B83;&#x60C5;&#x51B5;&#x5219;&#x6B63;&#x5E38;&#x6253;&#x5F00;&#x8DEF;&#x7531;&#x3002;</span>
     <span class="token punctuation">}</span>
   <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF0C;<code>onGenerateRoute</code>&#x53EA;&#x4F1A;&#x5BF9;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x751F;&#x6548;&#x3002;</p>
</blockquote>
<h2 id="227-&#x603B;&#x7ED3;">2.2.7 &#x603B;&#x7ED3;</h2>
<p>&#x672C;&#x7AE0;&#x5148;&#x4ECB;&#x7ECD;&#x4E86;Flutter&#x4E2D;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x3001;&#x4F20;&#x53C2;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x7136;&#x540E;&#x53C8;&#x7740;&#x91CD;&#x4ECB;&#x7ECD;&#x4E86;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x76F8;&#x5173;&#x5185;&#x5BB9;&#x3002;&#x5728;&#x6B64;&#x9700;&#x8981;&#x8BF4;&#x660E;&#x4E00;&#x70B9;&#xFF0C;&#x7531;&#x4E8E;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x53EA;&#x662F;&#x4E00;&#x79CD;&#x53EF;&#x9009;&#x7684;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x65B9;&#x5F0F;&#xFF0C;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x80FD;&#x5FC3;&#x4E2D;&#x4F1A;&#x72B9;&#x8C6B;&#x5230;&#x5E95;&#x4F7F;&#x7528;&#x54EA;&#x79CD;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x65B9;&#x5F0F;&#x3002;&#x5728;&#x6B64;&#xFF0C;&#x6839;&#x636E;&#x7B14;&#x8005;&#x7ECF;&#x9A8C;&#xFF0C;&#x5EFA;&#x8BAE;&#x8BFB;&#x8005;&#x6700;&#x597D;&#x7EDF;&#x4E00;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x7684;&#x7BA1;&#x7406;&#x65B9;&#x5F0F;&#xFF0C;&#x8FD9;&#x5C06;&#x4F1A;&#x5E26;&#x6765;&#x5982;&#x4E0B;&#x597D;&#x5904;&#xFF1A;</p>
<ol>
<li>&#x8BED;&#x4E49;&#x5316;&#x66F4;&#x660E;&#x786E;&#x3002;</li>
<li>&#x4EE3;&#x7801;&#x66F4;&#x597D;&#x7EF4;&#x62A4;&#xFF1B;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x533F;&#x540D;&#x8DEF;&#x7531;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x5728;&#x8C03;&#x7528;<code>Navigator.push</code>&#x7684;&#x5730;&#x65B9;&#x521B;&#x5EFA;&#x65B0;&#x8DEF;&#x7531;&#x9875;&#xFF0C;&#x8FD9;&#x6837;&#x4E0D;&#x4EC5;&#x9700;&#x8981;import&#x65B0;&#x8DEF;&#x7531;&#x9875;&#x7684;dart&#x6587;&#x4EF6;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x6837;&#x7684;&#x4EE3;&#x7801;&#x5C06;&#x4F1A;&#x975E;&#x5E38;&#x5206;&#x6563;&#x3002;</li>
<li>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>onGenerateRoute</code>&#x505A;&#x4E00;&#x4E9B;&#x5168;&#x5C40;&#x7684;&#x8DEF;&#x7531;&#x8DF3;&#x8F6C;&#x524D;&#x7F6E;&#x5904;&#x7406;&#x903B;&#x8F91;&#x3002;</li>
</ol>
<p>&#x7EFC;&#x4E0A;&#x6240;&#x8FF0;&#xFF0C;&#x7B14;&#x8005;&#x6BD4;&#x8F83;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x8DEF;&#x7531;&#xFF0C;&#x5F53;&#x7136;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x4EC0;&#x4E48;&#x91D1;&#x79D1;&#x7389;&#x5F8B;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x81EA;&#x5DF1;&#x504F;&#x597D;&#x6216;&#x5B9E;&#x9645;&#x60C5;&#x51B5;&#x6765;&#x51B3;&#x5B9A;&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E9B;&#x5173;&#x4E8E;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x7684;&#x5185;&#x5BB9;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x4ECB;&#x7ECD;&#xFF0C;&#x6BD4;&#x5982;&#x8DEF;&#x7531;MaterialApp&#x4E2D;&#x8FD8;&#x6709;<code>navigatorObservers</code>&#x548C;<code>onUnknownRoute</code>&#x4E24;&#x4E2A;&#x56DE;&#x8C03;&#x5C5E;&#x6027;&#xFF0C;&#x524D;&#x8005;&#x53EF;&#x4EE5;&#x76D1;&#x542C;&#x6240;&#x6709;&#x8DEF;&#x7531;&#x8DF3;&#x8F6C;&#x52A8;&#x4F5C;&#xFF0C;&#x540E;&#x8005;&#x5728;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x547D;&#x540D;&#x8DEF;&#x7531;&#x65F6;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x7531;&#x4E8E;&#x8FD9;&#x4E9B;&#x529F;&#x80FD;&#x5E76;&#x4E0D;&#x5E38;&#x7528;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x4FBF;&#x4E0D;&#x518D;&#x82B1;&#x8D39;&#x7BC7;&#x5E45;&#x6765;&#x4ECB;&#x7ECD;&#x4E86;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x81EA;&#x884C;&#x67E5;&#x770B;API&#x6587;&#x6863;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="first_flutter_app.html" class="navigation navigation-prev " aria-label="Previous page: 2.1：计数器示例">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="flutter_package_mgr.html" class="navigation navigation-next " aria-label="Next page: 2.3：包管理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.2：路由管理","level":"2.2.2","depth":2,"next":{"title":"2.3：包管理","level":"2.2.3","depth":2,"path":"chapter2/flutter_package_mgr.md","ref":"chapter2/flutter_package_mgr.md","articles":[]},"previous":{"title":"2.1：计数器示例","level":"2.2.1","depth":2,"path":"chapter2/first_flutter_app.md","ref":"chapter2/first_flutter_app.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","search-pro","ace","splitter","prism","donate","-highlight"],"root":"docs","styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"prism":{"lang":{"shell":"bash"}},"ace":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action@1.0.3/docs/imgs/pay.jpeg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"wendux 824783146@qq.com","bookRoot":"https://book.flutterchina.club/","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《Flutter实战》","language":"zh-hans","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"<div><img height=20 style='position:relative; top:5px; padding-right:5px ' src='https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/favicon.png'/> Flutter中文网</div>":"https://flutterchina.club"}},"gitbook":"*","description":"记录Gitbook的配置和一些插件的使用"},"file":{"path":"chapter2/flutter_router.md","mtime":"2019-11-25T08:35:49.892Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-15T14:52:22.710Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>



    <script src="https://pcdn.flutterchina.club/gitbook/gitbook.js"></script>
    <script src="https://pcdn.flutterchina.club/gitbook/theme.js"></script>
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-ace/ace/ace.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-ace/ace.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    


<script src="https://pcdn.flutterchina.club/gitbook/pub.js"></script>
<script>
    hookAjax({
        open: function (arg, xhr) {
            if (location.hostname !== 'localhost') {
                if (arg[1][0] === '.') arg[1] = arg[1].slice(1);
                arg[1] = "https://pcdn.flutterchina.club" + arg[1].replace(".html", ".1")
            }
        },
        setRequestHeader: function (arg, xhr) {
            console.log(arg[0], arg[1]);
            if (arg[0] !== 'Accept') return true;
        },
        onload:function(){
            setTimeout(addAD);
        }
    })
</script>
</body>

</html>

